layui.config({
    base: '/static/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index','set'], function() {
    $ = layui.jquery;
    var table = layui.table
        , admin = layui.admin
        , element = layui.element
        , layer = layui.layer;

    var tabIndex = 0;
    element.on('tab(theTab)', function(data){
        tabIndex = data.index;

        if(data.index == 0){

            getArticleData(2);

        }else if(data.index == 1){

            getVideData(2);

        }

    });

    $('#searchBtn').bind('click',function(){

        if(tabIndex == 0){

            getArticleData(1);

        }else if(tabIndex == 1){

            getVideData(1);

        }

    });

    function getArticleData(a){

       var keywords = $('#searchKeyWords').val();
       
       if (a==1){
           if (keywords == ''){
               layer.msg('请输入您要搜索的内容',{time:1000});
               return false;
           }
       }else{
           if (keywords == ''){
               return false;
           }
       }


        $.ajax({
            url: "/mobile/search/articleList",
            type: 'post',
            dataType: 'json',
            data: {article_title:keywords},
            success: function(data){
                if (data.article.length>0){

                    var article_str = '<ul id="article-list-li">';
                    $.each(data.article,function(k1,v1){
                        article_str += '<li>';
                        article_str += '<a href="/api/details/details?tid='+v1.id+'">';
                        article_str += '<div class="the-article">';
                        article_str += '<div class="article-l">';
                        article_str += '<h1>'+ v1.article_title +'</h1>';
                        article_str += '<p>' + v1.nickname + '·'+ v1.create_time +'</p>';
                        article_str += '</div>';
                        article_str += '<div class="article-r">';
                        article_str += '<img src="'+ v1.article_thumb +'" alt="">';
                        article_str += '</div>';
                        article_str += '<div class="clear"></div>';
                        article_str += '</div>';
                        article_str += '</a>';
                        article_str += '</li>';
                    });

                    article_str += '</ul>';

                    $('#article-list-div').html(article_str);

                }else{

                    var nostr = '<div class="nothing-img"><img src="/static/mobile/img/no-data.png"></div>';

                    $('#article-list-div').html(nostr);

                }

            }
        });

    }

    function getVideData(a){

        var keywords = $('#searchKeyWords').val();

        if (a==1){
            if (keywords == ''){
                layer.msg('请输入您要搜索的内容',{time:1000});
                return false;
            }
        }else{
            if (keywords == ''){
                return false;
            }
        }


        $.ajax({
            url: "/mobile/search/videoList",
            type: 'post',
            dataType: 'json',
            data: {title:keywords},
            success: function(data){

                if (data.video.length>0){
                    var video_str = '<ul id="video-list-li">';
                    $.each(data.video,function(k1,v1){
                        var r_num = v1.read_num*1+v1.set_read_num*1;
                        var send_num = v1.send_num*1+v1.set_send_num*1;
                        var collect_num = v1.collect_num*1+v1.set_collect_num*1;

                        video_str += '<li>';
                        video_str += '<a href="http://www.mygalawang.com/mobile/video/video?id='+ v1.id +'">';
                        video_str += '<div class="video-list-main">';
                        video_str += '<div class="video-img">';
                        video_str += '<img src="'+ v1.image +'" alt="">';
                        video_str += '</div>';
                        video_str += '<div class="video-title">';
                        video_str += '<h1>'+ v1.title +'</h1>';
                        video_str += '</div>';
                        video_str += '<div class="video-play">';
                        video_str += '<i></i>';
                        video_str += '</div>';
                        video_str += '<div class="video-info">';
                        video_str += '<div class="video-info-cen">';
                        video_str += '<div class="video-info-l">';
                        video_str += '<i></i>';
                        video_str += '<span id="look_num">'+ r_num +'</span>次观看';
                        video_str += '</div>';
                        video_str += '<div class="video-info-r">';
                        video_str += formatSeconds(v1.v_time);
                        video_str += '</div>';
                        video_str += '</div>';
                        video_str += '</div>';
                        video_str += '</div>';
                        video_str += '</a>';
                        video_str += '<div class="video-user">';
                        video_str += '<div class="video-user-name">'+ v1.nickname +'·'+ v1.create_time +'</div>';
                        video_str += '<div class="video-send-num"><i></i>'+send_num+'</div>';
                        video_str += '<div class="video-collect-num"><i></i>'+ collect_num +'</div>';
                        video_str += '<div class="clear"></div>';
                        video_str += '</div>';
                        video_str += '<div class="need-height-4"></div>';
                        video_str += '</li>';
                    });

                    video_str += '</ul>';

                    $('#video_list_div').html(video_str);
                }else{

                    var nostr = '<div class="nothing-img"><img src="/static/mobile/img/no-data.png"></div>';

                    $('#video_list_div').html(nostr);

                }

            }
        });

    }

    function formatSeconds(value) {
        var secondTime = parseInt(value);// 秒
        var minuteTime = 0;// 分
        var hourTime = 0;// 小时
        if(secondTime > 60) {//如果秒数大于60，将秒数转换成整数
            //获取分钟，除以60取整数，得到整数分钟
            minuteTime = parseInt(secondTime / 60);
            //获取秒数，秒数取佘，得到整数秒数
            secondTime = parseInt(secondTime % 60);
            //如果分钟大于60，将分钟转换成小时
            if(minuteTime > 60) {
                //获取小时，获取分钟除以60，得到整数小时
                hourTime = parseInt(minuteTime / 60);
                //获取小时后取佘的分，获取分钟除以60取佘的分
                minuteTime = parseInt(minuteTime % 60);
            }
        }
        var result = "" + parseInt(secondTime);

        if(minuteTime > 0) {
            result = "" + parseInt(minuteTime) + ":" + result;
        }
        if(hourTime > 0) {
            result = "" + parseInt(hourTime) + ":" + result;
        }
        return result;
    }

});

